﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Odbc;
using System.Data.Sql;
using System.Data.SqlClient;
namespace Data
{
    /// <summary>
    /// 线程池
    /// </summary>
    public class ThreadPool : IDisposable
    {
        Queue<Args.PoolArgs> m_pools;
        int capacity;
        public int Count { get { return m_pools != null ? m_pools.Count : 0; } }
        public Queue<Args.PoolArgs> GetPools { get { return m_pools; } }
        public ThreadPool(int poolsCount)
        {
            capacity = poolsCount;
            m_pools = new Queue<Args.PoolArgs>(capacity);
        }
        public void Push(Args.PoolArgs args)
        {
            if (args != null && m_pools.Count < capacity)
                lock (m_pools)
                {
                    m_pools.Enqueue(args);
                }
        }

        public Args.PoolArgs Pop()
        {
            if (m_pools.Count > 0)
            {
                lock (m_pools)
                {
                    return m_pools.Dequeue();
                }
            }
            else
            {
                return null;
            }
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                capacity = 0;
                m_pools.Clear();
                m_pools = null;
                GC.SuppressFinalize(this);
            }
        }

        public void Dispose()
        {
            Dispose(true);
        }
    }
}
